<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>生成器函数三个方法</title>
</head>

<body>
  <script>
    /*
      next()方法
      接收一个参数，这个参数会成为上一个yield表达式的返回值。
    */
    function* calculate() {
      let x = yield 1
      let y = yield 2
      let z = yield y + 2
      return y
    }
    var iterator = calculate()
    console.log(iterator.next());
    console.log(iterator.next(10));
    console.log(iterator.next(10));
    console.log("calculate");

    /* 
      return()方法
      提前终止当前生成器
      也可以跟随一个返回值
    */
    function* stop() {
      let x = yield 1;
      let y = yield x + 2;
      return y;
    }
    var iterator1 = stop()
    console.log(iterator1.next());
    console.log(iterator1.next());
    console.log(iterator1.next());  // 已经终止了当前生成器
    console.log("stop");

    /* 
      throw()方法
      强制生成器抛出一个错误。
      此方法也有一个参数，但这个参数只能被try-catch语句中的catch部分接收。
    */
    function* especial() {
      var count = 1;
      try {
        yield count = 1
      } catch(err) {
        count = 2
        console.log(err)
      }
      yield count + 3
    }

    var iterator2 = especial()
    var next1 = iterator2.next()
    console.log(next1)          // {value: 1, done: false}

    try {
      console.log(iterator2.throw("inner"))   // inner
      console.log(iterator2.next())           // {value: 5, done: false}
      console.log(iterator2.throw("outer"))   // {value: undefined, done: true}
    } catch (e) {
      console.log(e)                          // outer
    }

    console.log("especial")

    /* 
      yield*
      在yield关键字后面后跟一个星号（两边的空格可选），就能将执行权委托给另一个生成器或可迭代对象
    */
    function* generator() {
      var count = 0;
      while (count < 2) {
        yield count++
      }
      return count
    }

    function* delegation() {
      yield* ["a", "b"]
      var result = yield* generator()
      console.log(result)
    }

    var iterator3 = delegation()
    console.log(iterator3.next())
    console.log(iterator3.next())
    console.log(iterator3.next())
    console.log(iterator3.next())
    console.log(iterator3.next())
    console.log("iterator3")

    /* 
      异步编程
      回调函数  捕获Ajax通信中的响应内容
    */
    /* 
      ES6之前旧的方法
      function fetch1(callback) {
        $.getJSON("./resource/server.php", {}, function(json) {
          callback.call(this, json)
        })
      }

      function async() {
        fetch1(function(json) {
          console.log(json)
        })
      }
    */

    function fetch() {
      $.getJSON("./resource/server.php", {}, function(json) {
        gen.next(json)
      })
    }

    function* async() {
      var result = yield fetch()
      console.log(result)
    }

    var gen = async()
    gen.next()

    console.log("async")

    /* 
      电商项目运用
      获取商品清单 合计价格 付款 付款完成
    */
    function* gen(arg) {
      console.log(arg)
      let one = yield 111;
      console.log(one);

      let two = yield 222;
      console.log(two);

      let three = yield 333;
      console.log(three);
    }

    // 执行获取迭代器对象
    let iterator5 = gen('AAA');

    console.log(iterator5.next());  
    // next方法可以传入实参   实参作为 上一次 调用的返回值 返回
    console.log(iterator5.next('BBB'));
    console.log(iterator5.next('CCC'));   
    console.log(iterator5.next('DDD'));   // {value: undefined, done: true}
    console.log("gen");

  </script>
</body>

</html>